package com.sxf.tomcat;

import java.io.File;

import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.AprLifecycleListener;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardServer;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

public class LocalTomcat {
	private static int port = 80;
	private static final String WEB_ROOT = "src/main/webapp";

	public static void main(String[] args) throws Exception {
		System.setProperty("java.net.preferIPv4Stack", "true");
		// 得到hae目录
		File rootDir = new File(System.getProperty("user.dir")).getParentFile();
		rootDir = new File(rootDir, "wordpressMaven");

		Tomcat tomcat = new Tomcat();
		tomcat.setPort(port);
		
		String path = new File(rootDir, WEB_ROOT).getAbsolutePath();

		tomcat.getHost().setAppBase(path);
		StandardServer server = (StandardServer) tomcat.getServer();
		server.addLifecycleListener(new AprLifecycleListener());
		StandardContext ctx = (StandardContext) tomcat.addWebapp("/wp", path);

		// declare an alternate location for your "WEB-INF/classes" dir:
		File additionWebInfClasses = new File("target/classes");
		WebResourceRoot resources = new StandardRoot(ctx);
		resources.addPreResources(new DirResourceSet(resources,
				"/WEB-INF/classes", additionWebInfClasses.getAbsolutePath(),
				"/"));
		ctx.setResources(resources);

		tomcat.start();
		tomcat.getServer().await();
	}

}
